package com.huawei.bitfun.visitors;

import com.huawei.bitfun.Constants;
import com.huawei.bitfun.OptimizeManager;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;

public class DefaultVisitor extends PsiElementVisitor {
    private final ProblemsHolder holder;

    public DefaultVisitor(final ProblemsHolder holder) {
        this.holder = holder;
    }

    @Override
    public void visitElement(@NotNull PsiElement element) {
        super.visitElement(element);
    }

    @Override
    public void visitFile(@NotNull PsiFile file) {
        super.visitFile(file);
        var filePath = file.getVirtualFile().getCanonicalPath();
        if (!OptimizeManager.getInstance().hasOptimizeFile(filePath)) {
            return;
        }
        var documents = OptimizeManager.getInstance().getDocumentBeans(filePath);
        for (var document : documents) {
            if (Constants.REGISTERED_OPT.contains(document.getPass())) {
                continue;
            }
            var element = VisitorUtils.findElementAtPosition(file, document.getLine(), document.getColumn());
            if (element == null) {
                continue;
            }
            holder.registerProblem(element, document.getPass() + ": " + document.getName());
        }
    }
}